package org.example.node;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liyishan
 * @date 2023/12/6 10:00
 * @apiNote
 */

public class Test00234IsPalindrome {
    private ListNode frontPointer;

    public boolean isPalindrome(ListNode head) {
        List<Integer> vals = new ArrayList<>();
        ListNode currentNode = head;
        while (currentNode != null) {
            vals.add(currentNode.val);
            currentNode = currentNode.next;
        }
        int front = 0;
        int back = vals.size() - 1;
        while (front < back) {
            if (!vals.get(front).equals(vals.get(back))) {
                return false;
            }
            front++;
            back--;
        }
        return true;
    }


    public  boolean isPalindrome2(ListNode head) {
        frontPointer = head;
        return recursivelyCheck(head);
    }

    private boolean recursivelyCheck(ListNode head) {
        if(head != null){
            if(!recursivelyCheck(head.next)){
                return false;
            }
            if(head.val != frontPointer.val){
                return false;
            }
            frontPointer = frontPointer.next;
        }
        return true;
    }

}
